Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix extended model resolving #1949

Merged
merged 9 commits into from
Sep 17, 2024
Merged

Conversation

alecritson
Copy link
Collaborator

This PR looks to solve some anomalies that have been discovered with model extending.

Problem A

When replacing a Lunar model, there are instances where the model provided by Lunar is still used for example, if you replace the model like so

\Lunar\Facades\ModelManifest::replace(
    \Lunar\Models\Contracts\Order::class,
    \App\Models\Order::class
);

Later in the Lunar core, we are still referencing Lunar\Models\Order::first() or via relationships $orderLine->order(). We attempted to resolve these queries to their concrete replacements by overriding the newModelQuery method, however there were issues with castable attributes which caused them to be cast multiple times, resulting in errors.

Solution

This has been rewritten so Lunar doesn't try to fill the extended instance of the model from the Lunar base class anymore and instead just populates the attributes as they are. Since this should just be a simple class swap it should no longer result in duplicate calls to attribute casters.

There are some additional checks to see if we are actually working with a model that hasn't been extended to ensure we are not getting into a situation where we try and rehydrate with the same class.

Affected issues

This change should resolve #1942 #1930

Problem B

If your own custom model was named something other than it's counterpart, for example:

\Lunar\Facades\ModelManifest::replace(
    \Lunar\Models\Contracts\Order::class,
    \App\Models\MyCustomOrder::class
);

This would result in the table name and subsequent foreign key naming to be incorrect i.e. my_custom_order and my_custom_order_id. This would mean developers would need to add their own methods to override this in order for the naming to resolve properly, which is a bit of a maintenance burden and easily missed when encountering errors.

Solution

The HasModelExtending trait now provides its own getTable and getForeignKey methods which will check which class within Lunar we are extending and return the appropriate table name and foreign key.

How this slipped through testing

Looks like there was an oversight and although there were tests for extending models, the tests themselves didn't use any extending when performing more complex tasks, like creating orders from carts.

This PR has now added some model extending to tests which were affected by the issues referenced above to hopefully keep this in check and make for a more "real world" test environment.

Copy link

vercel bot commented Sep 12, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
lunar-docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Sep 17, 2024 8:34am

@ken717w
Copy link

ken717w commented Sep 16, 2024

Can't wait to see the next beta version 👍🏻. So far most of the issues I encountered are related to model extending.

glennjacobs
glennjacobs previously approved these changes Sep 16, 2024
@glennjacobs glennjacobs merged commit f3db113 into 1.x Sep 17, 2024
23 checks passed
@glennjacobs glennjacobs deleted the lun-156-unable-to-create-order branch September 17, 2024 08:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cannot update meta of existing OrderLine instance
3 participants